// pages/case-upload/case-upload.js
const {request} = require('../../utils/api.js');
import { ASSETS_BASE_URL, UPLOAD_BASE_URL } from '@/utils/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: [],
    show: false,
    area: '',
    title: '',
    description: '',
    p_id: '',
    a_id: '',
    c_id: '',
    addr: ''
  },
  showArea() {
    this.setData({
      show: true
    })
    console.log(111)
  },

  onClose() {
    this.setData({ show: false });
  },
  handleAreaCode(arr) {
    const obj = {}
    arr.forEach(item =>{
      let code = item.code.slice(0, 6)
      obj[code] = item.name
    })
    return obj
  },
  chooseImage(e) {
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {

        var path = res.tempFilePaths[0]
        wx.uploadFile({
          url: UPLOAD_BASE_URL + '/pic_uploads/name/file',
          header:{ "Content-Type":"multipart/form-data"},//类型
          filePath: path,
          name: 'file',
          success: function(data) {
            const imgList = that.data.images
            imgList.push(ASSETS_BASE_URL + data.data)
            that.setData({
              images: imgList
            })
            console.log(that.data.images);
            // 处理返回的数据
          },
          fail: function(err) {
            console.error('upload fail:', err);
          }
        });
      }
    });
  },
  close(e){
    this.setData({
      show:false,
      area:e.detail.value&&e.detail.value.join(","),
      p_id:e.detail.pid,
      c_id:e.detail.cid,
      a_id:e.detail.aid
    })
  },
  
  onSubmit() {
    const submitData = {
      member_id: wx.getStorageSync('user').member_id,
      title:  this.data.title,
      case_pics:  this.data.images,
      addr:  this.data.addr,
      p_id: this.data.p_id,
      c_id: this.data.c_id,
      a_id: this.data.a_id,
      description: this.data.description
    }
    if(!submitData.title){
      wx.showToast({
        title: '请输入案例名称',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(submitData.case_pics.length == 0){
      wx.showToast({
        title: '请添加图片',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    wx.showLoading({
      title: '',
    })
    request('upload_cases', 'POST', submitData).then(res => {
      console.log(res)
      wx.hideLoading()
      if (res.data.code != 200) {
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000
        })
        return;
      }
      wx.showToast({
        title: res.data.msg,
        icon: 'success',
        duration: 2000
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        })
      }, 1500)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getArea()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})